package com.example.edu.Service.Impl;

import com.example.edu.Dto.*;
import com.example.edu.Entity.Course;
import com.example.edu.Entity.User;
import com.example.edu.Mapper.Course_RecordMapper;
import com.example.edu.Mapper.DocumentMapper;
import com.example.edu.Mapper.UserMapper;
import com.example.edu.Service.SituationService;
import com.example.edu.Utils.CacheDataBaseUtil;
import com.example.edu.Utils.JwtUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import static com.example.edu.Utils.MyConstants.USER_CACHE_TTL;
import static com.example.edu.Utils.MyConstants.USER_KEY;

@Service
public class SituationServiceImpl implements SituationService {
    @Resource
    UserMapper userMapper;
    @Resource
    Course_RecordMapper course_recordMapper;
    @Resource
    DocumentMapper documentMapper;
    @Resource
    CacheDataBaseUtil cacheDataBaseUtil;

    @Override
    public Return timeAndWords(HttpServletRequest httpServletRequest, Course courseId) {
        String token = JwtUtil.getTokenFromCookie(httpServletRequest);
        String telephone = JwtUtil.getUsername(token);
        User user = cacheDataBaseUtil.selectById(USER_KEY,telephone, User.class, telephone2->userMapper.getUser(telephone2),
                USER_CACHE_TTL, TimeUnit.HOURS);
        if(user.getRole() == 0)
            return timeAndWordsT(user.getUser_id(),courseId);
        return timeAndWordsS(user.getUser_id(),courseId);
    }

    @Override
    public Return timePercent(HttpServletRequest httpServletRequest) {
        String token = JwtUtil.getTokenFromCookie(httpServletRequest);
        String telephone = JwtUtil.getUsername(token);
        User user = cacheDataBaseUtil.selectById(USER_KEY,telephone, User.class, telephone2->userMapper.getUser(telephone2),
                USER_CACHE_TTL, TimeUnit.HOURS);
        if(user.getRole() == 0) {
            List<Course_TimeDto> course_timeDtoList = course_recordMapper.getLearnTime(user.getUser_id());
            return returnPercent(course_timeDtoList);
        }
        List<Course_TimeDto> course_timeDtoList = course_recordMapper.getLearnTime2(user.getUser_id());
        return returnPercent(course_timeDtoList);
    }

    @Override
    public Return likeCollectView(HttpServletRequest httpServletRequest, Course courseId) {
        String token = JwtUtil.getTokenFromCookie(httpServletRequest);
        String telephone = JwtUtil.getUsername(token);
        User user = cacheDataBaseUtil.selectById(USER_KEY,telephone, User.class, telephone2->userMapper.getUser(telephone2),
                USER_CACHE_TTL, TimeUnit.HOURS);
        if(user.getRole() == 0)
            return likeCollectViewT(user.getUser_id(),courseId);
        return likeCollectViewS(user.getUser_id(),courseId);
    }

    @Override
    public Return rankingList(HttpServletRequest httpServletRequest, Course courseId) {
        String token = JwtUtil.getTokenFromCookie(httpServletRequest);
        String telephone = JwtUtil.getUsername(token);
        User user = cacheDataBaseUtil.selectById(USER_KEY,telephone, User.class, telephone2->userMapper.getUser(telephone2),
                USER_CACHE_TTL, TimeUnit.HOURS);
        if(user.getRole() == 0)
            return rankingListT(courseId.getCourse_id());
        return rankingListS(user.getUser_id(),courseId.getCourse_id());
    }

    private Return timeAndWordsT(int userId, Course courseId) {
        List<TimeWordsDtoR> records;
        if(courseId.getCourse_id() != null)
            records = course_recordMapper.getSituations(courseId.getCourse_id());
        else
            records = course_recordMapper.getSituations2(userId);
        if(records.isEmpty())
            return Return.fail(1,"还没有数据");
        return Return.ok(records);
    }

    private Return timeAndWordsS(int userId, Course courseId) {
        List<TimeWordsDtoR> records;
        if(courseId.getCourse_id() != null)
            records = course_recordMapper.getSituations3(courseId.getCourse_id(),userId);
        else
            records = course_recordMapper.getSituations4(userId);
        if(records.isEmpty())
            return Return.fail(1,"还没有数据");
        return Return.ok(records);
    }

    private Return returnPercent(List<Course_TimeDto> course_timeDtoList) {
        if(course_timeDtoList.isEmpty())
            return Return.fail(1,"还没有数据");
        double all_time = 0;
        for (Course_TimeDto course_timeDto : course_timeDtoList)
            all_time += course_timeDto.getS_time();
        if(all_time == 0)
            return Return.fail(1,"还没有数据");
        List<Course_TimePercentDtoR> rs = new ArrayList<>();
        for(Course_TimeDto course_timeDto : course_timeDtoList)
            rs.add(new Course_TimePercentDtoR(course_timeDto.getCourse_id(), course_timeDto.getCourse_name(), course_timeDto.getS_time() / all_time));
        return Return.ok(rs);
    }

    private Return likeCollectViewS(int id, Course courseId){
        if(courseId.getCourse_id() != null)
            return Return.ok(documentMapper.getData(id,courseId.getCourse_id()));
        return Return.ok(documentMapper.getData1(id));
    }

    private Return likeCollectViewT(int id, Course courseId){
        if(courseId.getCourse_id() != null)
            return Return.ok(documentMapper.getData2(courseId.getCourse_id()));
        return Return.ok(documentMapper.getData3(id));
    }

    private Return rankingListT(int courseId){
        List<TimeListDto> timeListData = course_recordMapper.getTimeList(courseId);
        List<WordsListDto> wordsListData = course_recordMapper.getWordsList(courseId);
        return Return.ok(new RankingListDtoR(timeListData,wordsListData));
    }

    private Return rankingListS(int student_id,int courseId){
        List<TimeListDto> timeListData = course_recordMapper.getTimeList(courseId);
        List<WordsListDto> wordsListData = course_recordMapper.getWordsList(courseId);
        for(TimeListDto time : timeListData){
            if(time.getStudent_id() == student_id)
                time.setIs_self(1);
        }
        for(WordsListDto words : wordsListData){
            if(words.getStudent_id() == student_id)
                words.setIs_self(1);
        }
        return Return.ok(new RankingListDtoR(timeListData,wordsListData));
    }
}
